package com.dkd;

import com.dkd.config.SpringBootRegister;
import com.dkd.controller.MainController;
import com.jfoenix.assets.JFoenixResources;
import com.jfoenix.controls.JFXDecorator;
import com.jfoenix.svg.SVGGlyph;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.container.DefaultFlowContainer;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Hello world!
 *
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, KafkaAutoConfiguration.class})
public class ToolFxApplication extends AbstractJavaFxApplicationSupport implements SmartInitializingSingleton, ApplicationContextAware
{


    private  static  final  ViewFlowContext flowContext =new ViewFlowContext() ;

    private ApplicationContext applicationContext;


    public static void main( String[] args )
    {
        new Thread(() -> launch(ToolFxApplication.class, args)).start();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // datafx
        Flow flow  = new Flow(MainController.class);
        flowContext.register("Stage", primaryStage);
        DefaultFlowContainer container = new DefaultFlowContainer();
        flow.createHandler(flowContext).start(container);
        // jfx
        JFXDecorator decorator = new JFXDecorator(primaryStage, container.getView());
        decorator.setCustomMaximize(true);
        decorator.setGraphic(new SVGGlyph(""));
        primaryStage.setTitle("工具2");
        double width = 1200;
        double height = 800;
        Scene scene = new Scene(decorator, width, height);
        final ObservableList<String> stylesheets = scene.getStylesheets();
        stylesheets.addAll(JFoenixResources.load("css/jfoenix-fonts.css").toExternalForm(),
                JFoenixResources.load("css/jfoenix-design.css").toExternalForm(),
                ToolFxApplication.class.getResource("/css/log-table-view.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    @Override
    public void afterSingletonsInstantiated() {

        SpringBootRegister.registerBean("applicationContext",applicationContext);
        SpringBootRegister.refresh();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
